package com.ruoyi.system.wnjk.consumer;

import com.ruoyi.system.pay.domain.dto.RefundRequestDTO;
import com.ruoyi.system.pay.domain.vo.PayRefundVO;
import com.ruoyi.system.pay.service.IRefundService;
import com.ruoyi.system.wnjk.config.RefundMQConfig;
import com.ruoyi.system.wnjk.contents.Contents;
import com.ruoyi.system.wnjk.domain.po.RefundMessage;
import com.ruoyi.system.wnjk.domain.po.ShopAfterSalesRequests;
import com.ruoyi.system.wnjk.service.IShopAfterSalesRequestsService;
import com.ruoyi.system.wnjk.service.IShopOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RefundConsumer {

    @Autowired
    private IShopOrderService shopOrderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IRefundService refundService;

    @Autowired
    private IShopAfterSalesRequestsService shopAfterSalesRequestsService;

    /**
     * 消费退款消息
     * @param message 退款消息
     */
    @RabbitListener(queues = RefundMQConfig.REFUND_QUEUE)
    public void processRefundRequest(RefundMessage message) {
        System.out.println(" [x] Received refund request: " + message);

        try {
            //退款
//            orderService.processRefund(message.getOrderId(), message.getRefundRequestId());
            RefundRequestDTO refundRequestDTO = new RefundRequestDTO();
            refundRequestDTO.setOrderNumber(message.getOrderId());
            refundRequestDTO.setSourceType(message.getOrderType());
            refundRequestDTO.setOutRequestNo(message.getTradeNo());
            refundRequestDTO.setRefundReason(message.getReason());
            refundRequestDTO.setRefundAmount(message.getAmount());
            log.info("退款信息:"+refundRequestDTO);
            //获取退款信息
            PayRefundVO payRefundVO = refundService.unifiedRefund(refundRequestDTO);
            if (payRefundVO.getCode().equals(Contents.Pay.REFUND_STATUS_SUCCESS)){
                //退款成功
                //数据处理
                processData(payRefundVO);
            }else if (payRefundVO.getCode().equals(Contents.Pay.SYSTEM_STATUS_BUSY)){
                //系统繁忙，重新发送消息
                rabbitTemplate.convertAndSend(RefundMQConfig.REFUND_EXCHANGE, RefundMQConfig.REFUND_QUEUE, message);
            }

        } catch (Exception e) {
            System.err.println("Failed to process refund request: " + message);
            rabbitTemplate.convertAndSend(RefundMQConfig.REFUND_EXCHANGE, RefundMQConfig.REFUND_QUEUE, message);
            e.printStackTrace();
        }
    }

    private void processData(PayRefundVO payRefundVO) {
        if (payRefundVO.getSourceType().equals(Contents.Pay.TYPE_GW_SHOP)){
            //购物
            ShopAfterSalesRequests shopAfterSalesRequests = new ShopAfterSalesRequests();
            shopAfterSalesRequests.setOrderId(Long.parseLong(payRefundVO.getOrderNumber()));
            List<ShopAfterSalesRequests> salesRequests = shopAfterSalesRequestsService.selectShopAfterSalesRequestsList(shopAfterSalesRequests);
            salesRequests.forEach(item ->{
                item.setComment("已退款(累计):"+payRefundVO.getRefundFee());
                shopAfterSalesRequestsService.updateShopAfterSalesRequests(item);
            });
        } else if (payRefundVO.getSourceType().equals(Contents.Pay.TYPE_GH_REGISTRATION)) {
            //挂号

        }else if (payRefundVO.getSourceType().equals(Contents.Pay.TYPE_WZ_INTERROGATION)) {
            //问诊

        } else if (payRefundVO.getSourceType().equals(Contents.Pay.TYPE_TJ_PHYSICAL)) {
            //体检

        }
    }
}
